package com.douma.lock;

import redis.clients.jedis.Jedis;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 抖码课堂，专注中国程序员基础内功修炼
 * 官网：douma-tech.com
 * 公众号：抖码课堂
 *
 * @作者 : 老汤
 */
public class TestDistributeLock {

    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.126.130", 6381);
        jedis.auth("123456");

        String uuid = UUID.randomUUID().toString();
        RedisDistributeLock6 lock = new RedisDistributeLock6(uuid, jedis);

        try {
            // 锁重试
            while (!lock.lock(30)) {
                TimeUnit.MILLISECONDS.sleep(100);
            }

            System.out.println("do something ...");
            TimeUnit.SECONDS.sleep(10);

        } catch (InterruptedException e) {
          e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
